package com.jishuo.nontax.bfc.service;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.service.SequenceService;
import com.jishuo.nontax.bean.vo.BankPosNewVO;
import com.jishuo.nontax.bfc.dao.BankPosNewMapper;
import com.jishuo.nontax.bfc.pzk.PzkService;
import com.jishuo.nontax.bfc.tlq.MessageFactory;
import com.jishuo.nontax.bfc.tlq.MessageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @auther dinggaofei
 * @Date 2022/12/21 14:10
 */
@Service
public class BankPosNewService {


    private static Logger logger = LoggerFactory.getLogger(BankPosNewService.class);

    @Resource
    private BankPosNewMapper bankPosNewMapper;
    @Resource
    private MessageService messageService;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private NonTaxConfig nonTaxConfig;

    /**
     * @param
     * @auther dinggaofei
     * @Date 2022/12/21 14:35
     * @description 查询新pos信息 发送到财政
     */
    public BaseResult sendNewBankPos(String accountcode, String paybankdt) {
        try {
            paybankdt = paybankdt.substring(0, 10);
            ArrayList<BankPosNewVO> list =bankPosNewMapper.findSendNewBankPos(accountcode, paybankdt);
            if (list != null && list.size() > 0) {
                long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");
                Object setBankPosNewDetail = MessageFactory.getSetBankPosNewDetail(list);
                //发送到财政
                messageService.sendMessage("SETBANKPOSDETAIL", messageid, setBankPosNewDetail, "2.4");
                // 写凭证
                if (nonTaxConfig.IS_Pzk) {
                    PzkService.sendPzk("SETBANKNEWPOSDETAILPZK", setBankPosNewDetail);
                }

            }
            return new BaseResult();
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }


    /**
     * @param bankPosNew 新pos对象
     * @auther dinggaofei
     * @Date 2022/12/21 14:05
     * @description 记录 账户POS明细
     */
        public BaseResult setNewBankPos(BankPosNewVO bankPosNew) {
        try {
            if (bankPosNewMapper.findNewBankPosCount(bankPosNew.getBankaccount(), bankPosNew.getOpaccountnum(), bankPosNew.getPaybankdt()) > 0) {
                if (bankPosNewMapper.updateNewBankPos(bankPosNew) > 0) {
                    return new BaseResult();
                }
            } else {
                if (bankPosNewMapper.insertNewBankPos(bankPosNew) > 0) {
                    return new BaseResult();
                }
            }
            return new BaseResult(ErrorCodeEnum.DB_ERR);
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }


}
